package com.csx.governance.traffic.adm.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cuisongxu
 * @date 2023/4/24 10:01
 */
public class ModelConvertUtil {

    public static <S,D> D convert(S sourceObj, Class<D> destClazz) {

        if(ObjectUtil.isEmpty(sourceObj)) {
            return null;
        }
        D destObj = null;
        try {
            destObj = destClazz.newInstance();
            BeanUtil.copyProperties(sourceObj, destObj);
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return destObj;
    }
    public static <S,D> List<D> convert(List<S> sourceList, Class<D> destClazz) {

        if(CollectionUtil.isEmpty(sourceList)) {
            return new ArrayList<>();
        }
        return sourceList.stream().map(source -> {
            D dest = null;
            try {
                dest = destClazz.newInstance();
                BeanUtil.copyProperties(source, dest);
            } catch (InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
            }
            return dest;
        }).collect(Collectors.toList());
    }


}
